var app = getApp();
import commonAPI from '../../api/api'
const FN = require('../../utils/public');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mywordArr: [],
    wordinfoArr: [],
    appWidth: 320,
    loaddingOver: false,
    x: 0,
    wordNum: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.hideShareMenu(); // 禁止分享
    that.getMycol();
    app.getappsysteminfo(function (res) {
      that.setData({
        appWidth: res.windowWidth
      });
    });
  },

  // 跟读单词
  repeatWord: function (e) {
    var that = this,
      errorArr = new Array(),
      errorObj = {},
      currError = e.currentTarget.dataset.errorword;
    // 将单词放入数组
    for (var i in that.data.wordinfoArr) {
      var obj = {};
      obj.errorWords = that.data.wordinfoArr[i].collectionContent;
      obj.voiceSystemId = that.data.wordinfoArr[i].voiceSystemId;
      if (that.data.wordinfoArr[i].collectionContent == e.currentTarget.dataset.errorword) {
        errorArr.unshift(obj);
      } else {
        errorArr.push(obj);
      }
    }
    errorObj.errWords = errorArr;
    wx.navigateTo({
      url: '../errorWords/errorWords?errorwords=' + JSON.stringify(errorObj)
    })
  },

  // 去练习
  repeat2Index: function () {
    wx.switchTab({
      url: '../bookList/bookList'
    })
  },

  // 取消收藏 colDelCol
  delWord: function (e) {
    var that = this;
    var collectionContent = e.currentTarget.dataset.word;
    commonAPI.colDelCol({ "collectionContent": collectionContent }).then(res => {
      if (res.code == '200') {
        that.setData({
          x: 0
        })
        that.getMycol();
      }
    })
  },

  // 我收藏的单词
  getMycol: function () {
    var that = this;
    commonAPI.getMycol({ }).then(res => {
      if (res.code == '200') {
        var temArr = res.data;
        var dateArr = new Array();
        // 获取日期到数组
        for (var i in temArr) {
          dateArr.push(temArr[i].collectionDate);
        }

        // 去重复
        var newdateArr = Array.from(new Set(dateArr));
        var newWordInfoArr = new Array();
        for (var i in newdateArr) {
          var obj = {};
          obj.time = newdateArr[i];
          obj.wordInfoArr = [];
          newWordInfoArr.push(obj);
        }
        // 数据分装以时间为纬度
        for (var i in newWordInfoArr) {
          var wordObj = {};
          for (var j in temArr) {
            if (newWordInfoArr[i].time == temArr[j].collectionDate) {
              newWordInfoArr[i].wordInfoArr.push(temArr[j]);
            }
          }
        }
        that.setData({
          mywordArr: newWordInfoArr,
          wordNum: temArr.length,
          wordinfoArr: temArr,
          loaddingOver: true
        })
      }
    })
  }
})